import pygame
import sys

# 初始化pygame
pygame.init()

# 设置窗口大小
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("控制圆移动和跳跃")

# 定义颜色
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GRAVITY = 0.5  # 重力加速度

# 定义圆的初始位置和速度
circle_radius = 30
circle_x = width // 2
circle_y = height - circle_radius
circle_speed_x = 0
circle_speed_y = 0
is_jumping = False

# 主循环
clock = pygame.time.Clock()
while True:
    screen.fill(WHITE)  # 清屏

    # 处理事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        # 键盘按下事件
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                circle_speed_x = -5  # 向左移动
            if event.key == pygame.K_RIGHT:
                circle_speed_x = 5  # 向右移动
            if event.key == pygame.K_UP and not is_jumping:
                circle_speed_y = -15  # 跳跃初速度
                is_jumping = True

        # 键盘松开事件
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT and circle_speed_x < 0:
                circle_speed_x = 0  # 停止左移
            if event.key == pygame.K_RIGHT and circle_speed_x > 0:
                circle_speed_x = 0  # 停止右移

    # 更新圆的位置
    circle_x += circle_speed_x
    circle_y += circle_speed_y

    # 重力效果
    if is_jumping:
        circle_speed_y += GRAVITY
        if circle_y >= height - circle_radius:  # 落地
            circle_y = height - circle_radius
            circle_speed_y = 0
            is_jumping = False

    # 边界检测
    if circle_x < circle_radius:
        circle_x = circle_radius
    if circle_x > width - circle_radius:
        circle_x = width - circle_radius

    # 绘制圆
    pygame.draw.circle(screen, BLUE, (int(circle_x), int(circle_y)), circle_radius)

    # 刷新屏幕
    pygame.display.flip()
    clock.tick(60)  # 60帧每秒